﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Business.Reservations;

namespace blackhouse.BlackApart.Business.ApartmentServices {
    public class ReservationAdditionalServicesOrder {

        #region Fields

        private IWebReservation reservation;
        private Dictionary<int, IAdditionalService> services;
        private Dictionary<int, ReservationAdditionalService> reservationServices;
        private Factory factory = Factory.Instance();

        #endregion

        #region Properties

        public double Value {
            get {
                if (this.reservationServices != null && this.reservationServices.Count > 0)
                    return this.reservationServices.Sum(ras => ras.Value.Price);
                else
                    return 0;
            }
        }

        #endregion

        #region Constructors

        public ReservationAdditionalServicesOrder(IWebReservation forReservation) {
            this.reservation = forReservation;
            this.SetServices();
            this.SetReservationServices();
        }

        #endregion

        #region Methods

        private void SetServices() {
            IAdditionalService[] services = this.factory.GetAdditionalService().GetServicesForReservation(this.reservation.ReservationId);
            this.services = new Dictionary<int, IAdditionalService>(services.Length);
            foreach (IAdditionalService service in services) {
                this.services.Add(service.ServiceId, service);
            }
        }

        private void SetReservationServices() {
            if (services != null) {
                this.reservationServices = new Dictionary<int, ReservationAdditionalService>(this.services.Count);
                foreach (KeyValuePair<int, IAdditionalService> service in this.services) {
                    ReservationAdditionalService reservationService = service.Value.GetOrderFromReservation(this.reservation);
                    if (reservationService != null && reservationService.Quantity > 0)
                        this.reservationServices.Add(service.Key, reservationService);
                }
            }
        }

        public IEnumerable<IAdditionalService> GetOrderingServices() {
            return this.reservationServices.Select(ras => ras.Value.Service);
        }

        public IEnumerable<ReservationAdditionalService> GetOrderingReservationServices() {
            return this.reservationServices.Values;
        }

        public ReservationAdditionalService GetReservationService(IAdditionalService service) {
            if (service != null)
                return this.GetReservationService(service.ServiceId);
            else
                return null;
        }

        public ReservationAdditionalService GetReservationService(int serviceId) {
            if (this.reservationServices.ContainsKey(serviceId))
                return this.reservationServices[serviceId];
            else
                return null;
        }

        #endregion

    }
}
